package supplier

import (
	"context"
	"net/http"
	"wms/model"

	"wms/internal/svc"
	"wms/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SupplierAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSupplierAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SupplierAddLogic {
	return &SupplierAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SupplierAddLogic) SupplierAdd(req *types.SupplierAddRequest) (resp *types.BaseResponse, err error) {
	resp = new(types.BaseResponse)
	//1.供货商是否存在
	_, err = l.svcCtx.SupplierModel.FindOneByName(l.ctx, req.Name)
	if err != model.ErrNotFound { //内部错误、供货商已存在
		resp.Code = http.StatusBadRequest
		resp.Msg = "供货商已存在"
		return
	}

	//2.录入供货商信息
	_, err = l.svcCtx.SupplierModel.Insert(l.ctx, &model.Supplier{
		Name: req.Name,
	})
	if err != nil {
		resp.Code = http.StatusInternalServerError
		resp.Msg = "服务器内部错误"
		return
	}

	resp.Code = http.StatusOK
	resp.Msg = "成功"
	return
}
